import requests
from bs4 import BeautifulSoup
import bs4

# https://stackoverflow.com/a/30070664

ranges = [
    [ord(u"\u3300"), ord(u"\u33ff")],  # compatibility ideographs
    [ord(u"\ufe30"), ord(u"\ufe4f")],  # compatibility ideographs
    [ord(u"\uf900"), ord(u"\ufaff")],  # compatibility ideographs
    [ord(u"\U0002F800"), ord(u"\U0002fa1f")],  # compatibility ideographs
    [ord(u'\u3040'), ord(u'\u309f')],  # Japanese Hiragana
    [ord(u"\u30a0"), ord(u"\u30ff")],  # Japanese Katakana
    [ord(u"\u2e80"), ord(u"\u2eff")],  # cjk radicals supplement
    [ord(u"\u4e00"), ord(u"\u9fff")],
    [ord(u"\u3400"), ord(u"\u4dbf")],
    [ord(u"\U00020000"), ord(u"\U0002a6df")],
    [ord(u"\U0002a700"), ord(u"\U0002b73f")],
    [ord(u"\U0002b740"), ord(u"\U0002b81f")],
    [ord(u"\U0002b820"), ord(u"\U0002ceaf")]  # included as of Unicode 8.0
]

is_cjk = lambda char: any([r[0] <= ord(char) <= r[1] for r in ranges])


def get_width(s):
    cnt = 0
    for c in s:
        if is_cjk(c):
            cnt += 2
        else:
            cnt += 1
    return cnt


# 返回 拉取的网站内容
def getHTMLText(url):
    try:
        r = requests.get(url, timeout=30)
        r.raise_for_status()
        r.encoding = 'utf-8'
        return r.text
    except:
        return ""


# 得到对应表格，每一个标签的
def getTableFrom(ns_of_table):
    table = []
    for tr in ns_of_table.children:
        if isinstance(tr, bs4.element.Tag):
            tds = []
            for td in tr:
                if isinstance(td, bs4.element.Tag):
                    tds.append((td.string).strip())
            table.append(tds)
    return table


def sortedTable(t, key_idx, reverse):
    try:
        float(t[0][key_idx])
        return sorted(t, key=lambda x: float(x[key_idx]), reverse=reverse)
    except:
        return sorted(t, key=lambda x: x[key_idx], reverse=reverse)


def printTable(header, t):
    tot_cols = len(header)
    tot_rows = len(t)

    table = [header] + t
    width_of_table = [[get_width(s) for s in r] for r in table]
    width_each_col = [max([r[i] for r in width_of_table]) for i in range(tot_cols)]

    for i in range(tot_rows + 1):
        for j in range(tot_cols):
            print(table[i][j], end=' ' * (width_each_col[j] - width_of_table[i][j] + 1))
        print()


url = r"http://www.gaosan.com/gaokao/43980.html"
html_text = getHTMLText(url)
print("=====================")
print(html_text)
print("===================")
# html_text = open('Untitled-1.html').read()

soup = BeautifulSoup(html_text, "html.parser")

navigable_string_of_table = soup.find('tbody')

colleges_table = getTableFrom(navigable_string_of_table)

table_header = colleges_table[0]
table_content = colleges_table[1:]


# print('Sorted by: ')
# i = 1
# for index in table_header:
#     print("{}.".format(i), index)
#     i += 1
# print("{}.".format(i), 'Quit')

# c = int(input())
# while 0 < c < i:
#     printTable(table_header, sortedTable(table_content, c-1, False))
#     c = int(input())

printTable(table_header, sortedTable(table_content, table_header.index('社会影响'), False))

print('Bye!')
